#!/bin/sh
#Usage: directories need to be named with phylum names, such as cyano or chlorobi
#Eg: dissertation_SingleCopyGenes.sh genes.list cyano

genes=$1
phylum=$2
count=`wc -l $genes | awk '{print $1}'`

#Doing BLAST
for i in `cat $genes`;do
    echo "Working on $i BLAST for $phylum"
    cd /host/Users/JS/UH-work/gloeobacter/final_work/comparisons/orthologs/$phylum
    for j in `cat $phylum.list`;do
        blastall -p blastp -i /host/Users/JS/UH-work/gloeobacter/final_work/comparisons/coregene_trees/$i.GKIL.fasta -d $j.refseq.faa -F F -e 0.01 -m 8 -o $i.$j.blastp
        head -1 $i.$j.blastp | awk '{print $2}' > tmp.out
        exfasta `cat tmp.out` $j.refseq.faa > $i.$j.fasta
        cat $i.$j.fasta >> $j.$count.fasta
        echo ">core$j" > dummy.tmp
        sed -i 's/>.*//g' $j.$count.fasta
        cat dummy.tmp $j.$count.fasta > $j.tmp
        bp_seqconvert.pl --from fasta --to fasta < $j.tmp > $j.$count.fasta
        rm -f dummy.tmp $j.tmp tmp.out
    done
    cat *.$count.fasta > $phylum.core$count.fasta
    echo "Done with $i from $phylum"
done
